package cn.jiedh.app.meiweidk.mine;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.bigkoo.pickerview.OptionsPickerView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.jiedh.app.meiweidk.R;
import cn.jiedh.app.meiweidk.Utils.CityBaseBean;
import cn.jiedh.app.meiweidk.Utils.CityNameInterface;
import cn.jiedh.app.meiweidk.Utils.CityUtils;
import cn.jiedh.app.meiweidk.Utils.UI.CustomItemLayout;
import cn.jiedh.app.meiweidk.Utils.tool.ActivityUtil;
import cn.jiedh.app.meiweidk.Utils.tool.Constans;
import cn.jiedh.app.meiweidk.Utils.tool.L;
import cn.jiedh.app.meiweidk.Utils.tool.StringUtil;
import cn.jiedh.app.meiweidk.Utils.tool.StringUtils;
import cn.jiedh.app.meiweidk.Utils.tool.T;
import cn.jiedh.app.meiweidk.app.MyApp;
import cn.jiedh.app.meiweidk.base.BaseActivity;
import cn.jiedh.app.meiweidk.entity.UserInfoBean;

/**
 * @name： 基本资料
 * @author：彭鑫
 * @email：pengwwe457621023@qq.com
 * @createTime：2018/11/17
 */

public class BasicInfoActivity extends BaseActivity {

    @BindView(R.id.marri_cil)
    CustomItemLayout marri_cil;//婚姻情况
    @BindView(R.id.income_cil)
    CustomItemLayout income_cil;//月均收入
    @BindView(R.id.job_cil)
    CustomItemLayout job_cil;//职业
    @BindView(R.id.priceType_cil)
    CustomItemLayout priceType_cil;//工资发放形式
    @BindView(R.id.city_cil)
    CustomItemLayout city_cil;//城市
    @BindView(R.id.credit_cil)
    CustomItemLayout credit_cil;//信用状况


    private OptionsPickerView marriPickerView;
    private int marriType = 99;

    private OptionsPickerView incomePickerView;
    private int incomeType = 99;

    private OptionsPickerView jobPickerView;
    private int jobType = 99;

    private OptionsPickerView priceTypePickerView;
    private int priceTypeType = 99;

    private OptionsPickerView creditPickerView;
    private int creditType = 99;

    private String city;


    @BindView(R.id.edu_et)
    EditText edu_et;

    private UserInfoBean userInfoBean;


    @Override
    protected int getLayoutView() {
        return R.layout.activity_basicinfo;
    }

    @Override
    protected void initView(View view) {
        setTitle("基本资料");
    }

    @Override
    protected void initData() {
        MyApp.getInstance().addActivity(this);
        userInfoBean= (UserInfoBean) getIntent().getExtras().getSerializable("userinfo");
    }

    /**
     * 构造pickerview
     *
     * @param itemLayout
     * @param pickerView
     * @param title
     * @param values
     */
    private void buildPicker(CustomItemLayout itemLayout, OptionsPickerView pickerView, String title, String... values) {

        if (values == null || values.length == 0) {
            return;
        }
        if (pickerView != null && pickerView.isShowing()) {
            pickerView.dismiss();
        }

        if (pickerView == null) {
            pickerView = new OptionsPickerView.Builder(this, (options1, options2, options3, v) -> {
                itemLayout.setValue(values[options1]);
                switch (itemLayout.getId()) {
                    case R.id.marri_cil:
                        marriType = options1;
                        L.i("marriType===" + marriType);
                        break;

                    case R.id.income_cil:
                        incomeType = options1;
                        L.i("incomeType===" + incomeType);
                        break;

                    case R.id.job_cil:
                        jobType = options1;
                        L.i("jobType===" + jobType);
                        break;

                    case R.id.priceType_cil:
                        priceTypeType = options1;
                        L.i("priceTypeType===" + priceTypeType);
                        break;

                    case R.id.credit_cil:
                        creditType = options1;
                        L.i("creditType===" + creditType);
                        break;

                }
            }).setOutSideCancelable(false)
                    .setDividerColor(R.color.hint3_text_color)
                    .setTextColorCenter(Color.BLACK) //设置选中项文字颜色
                    .setContentTextSize(20)
                    .setTitleText(title)
                    .setTitleColor(Color.BLACK)
                    .build();
            pickerView.setPicker(Arrays.asList(values));
        }
        pickerView.show();
    }


    @OnClick({R.id.authen_next, R.id.marri_cil, R.id.income_cil, R.id.job_cil, R.id.priceType_cil, R.id.city_cil, R.id.credit_cil})
    public void Click(View v) {
        switch (v.getId()) {
            case R.id.authen_next:
                if (checkData()) {
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("userinfo", userInfoBean);
                    ActivityUtil.next(this, SuppleInfoActivity.class,bundle);
                }
//                decodeCity();
                break;

            case R.id.marri_cil:
                buildPicker(marri_cil, marriPickerView, "婚姻状况", PersonInfo.getMarryTypes());
                break;

            case R.id.income_cil:
                buildPicker(income_cil, incomePickerView, "月均收入", PersonInfo.get2IncomeTypes());
                break;
            case R.id.job_cil:
                buildPicker(job_cil, jobPickerView, "职业", PersonInfo.get2JobTypes());
                break;
            case R.id.priceType_cil:
                buildPicker(priceType_cil, priceTypePickerView, "工资发放形式", PersonInfo.getPriceTypes());
                break;
            case R.id.city_cil:

                CityUtils.getInstance().getAllCity(this, area -> {
                    city = area;
                    city_cil.setValue(area);
                });
                break;
            case R.id.credit_cil:
                buildPicker(credit_cil, creditPickerView, "信用情况", PersonInfo.getcreditTypes());
                break;

        }
    }


    private boolean checkData() {
        if (marriType == 99) {
            T.show("请选择婚姻状况");
            return false;
        } else if (incomeType == 99) {
            T.show("请选择月均收入");
            return false;
        } else if (jobType == 99) {
            T.show("请选择职业");
            return false;
        } else if (priceTypeType == 99) {
            T.show("请选择工资发放形式");
            return false;
        } else if (StringUtil.isEmpty(city)) {
            T.show("请选择所在城市");
            return false;
        } else if (creditType == 99) {
            T.show("请选择信用情况");
            return false;
        }


        int amount= Integer.parseInt(edu_et.getText().toString());

        if(amount<2000||amount>50000){
            T.show("贷款额度在2000到5万之间");
            return false;
        }

        userInfoBean.setAmount(edu_et.getText().toString());
        userInfoBean.setMaritalStatus(marriType);
        userInfoBean.setMonthlyIncome(incomeType);
        userInfoBean.setJobType(jobType);
        userInfoBean.setPayoff(priceTypeType);
        userInfoBean.setCityLive(city);
        userInfoBean.setCredit(creditType);

        return true;
    }

    public void decodeCity() {

        String sheng = null;

        String shi = null;

        List<CityBaseBean> beans = new Gson().fromJson(Constans.cityString2, new TypeToken<List<CityBaseBean>>() {
        }.getType());


        for (CityBaseBean bean : beans) {
            shi = "";
//            sheng+=bean.getName()+",";
            for (CityBaseBean.CityBean cityBean : bean.getCity()) {
                shi += cityBean.getName() + ",";
            }
            L.i("\"" + shi + "\",");
        }

    }


}
